package library.controller;

import com.j256.ormlite.stmt.PreparedQuery;
import library.entity.Book;
import library.entity.BookStatus;

import java.sql.SQLException;
import java.util.List;

public class BookController extends AbstractController<Book>  {
	//private static Dao<Book, String> bookDao;
	List<Book> result;
  //  ErrorMessage lastError;

	public BookController() throws SQLException{
		super(Book.class);
	}

    /**
     *  Search the book by keyword
     * @param keyword The word phase that want to getLibrarian
     * @return A Collection of Book that contain the keyword
     * @throws SQLException
     */
	public List<Book> search(String keyword) throws SQLException{
		if(result != null)
			result.clear();
		PreparedQuery<Book> preparedQuery = getDao().queryBuilder().where().like("name", "%" + keyword + "%").prepare();
		result = getDao().query(preparedQuery);
		return this.result;
	}
	
	public void updateStatus(Book book, BookStatus status) throws SQLException{
		Book objBook = selectById(book);
		objBook.setStatus(status);
		update(objBook);
	}

   /**
     *  Find the book by id
     * @param id The book id that want to getLibrarian
     * @return A Book object
     */
	public Book getBookById(int id){
		try {
            return selectById(id);
		} catch (Exception ex) {
			return null;
		}
	}
		
	public boolean createNewBook(Book book){
		return insert(book);
	}

    public boolean deleteBook(Book book){
    	return delete(book);
    }

}
